<template>
	<view>
		<view class="baseForm">
			<u-list @scrolltolower="scrolltolower" class="list">
				<u-list-item v-for="(item, index) in indexList1" :key="item.id">
					<view class="card">
						<view class="right">
							<view class="title">
								<view class="label">设备编号：</view>
								<view class="content">{{ item.equipCode || '暂无原因' }}</view>
							</view>
							<view class="title">
								<view class="label">产品：</view>
								<view class="content">{{ item.productCode || '暂无原因' }}</view>
							</view>
							<view class="title">
								<view class="label">位置：</view>
								<view class="content">{{ item.equipPosition || '暂无原因' }}</view>
							</view>
						</view>
						<view class="left">
							<button v-show="item?.isSelect" class="uni-btn" size="mini" type="primary">已选择</button>
							<button v-show="!item?.isSelect" class="uni-btn" size="mini" plain type="primary" @click="selectWorkOrder(item, index)">选 择</button>
						</view>
					</view>
				</u-list-item>
			</u-list>
		</view>
		<u-modal
			:show="showImport"
			ref="uModal"
			:content="`当前设备编号为${selectOrder?.equipCode ?? ''}, 请确认是否选择？`"
			confirmText="确认"
			showCancelButton
			@cancel="cancelImport"
			@confirm="confirmImport"
		></u-modal>
	</view>
</template>

<script setup lang="ts">
import { computed, ref, watch, reactive } from 'vue';
import { onPageScroll, onPullDownRefresh, onShow } from '@dcloudio/uni-app';
import ScanBar from '@/components/scan-bar/index.vue';
import { onLoad } from '@dcloudio/uni-app';
import { http } from '@/utils/http';
import { PageResult } from '@/types/type';
import { useUserStore } from '../../../../stores/useUserStore';
const current = ref(0); // 当前页
const totalPage = ref(0); //累计分页
const total = ref(0); // 累计条数
const scanBarRef = ref();
// 监听页面到顶部距离
const scrollTop = ref(0);
//列表高度
const listHeight = ref('');
const scrolltolower = () => {
	totalPage.value > current.value;
};
onPageScroll((e) => {
	scrollTop.value = e.scrollTop;
});
onShow(async () => {
	// #ifdef H5
	listHeight.value = 'calc(100vh - 88px)';
	// #endif

	// #ifdef APP-PLUS
	listHeight.value = 'calc(100vh - 44px)';
	// #endif
});
const props = defineProps({
	id: {
		type: String,
		default: ''
	},
	code: {
		type: String,
		default: ''
	}
});

const indexList = ref([
	{
		id: '1',
		consumeCode: '123',
		equipPosition: '办公室',
		materialCode: '物料编号1',
		equipCode: '设备编号1'
	},
	{
		id: '2',
		consumeCode: '1234',
		equipPosition: '厂房',

		materialCode: '物料编号2',
		equipCode: '设备编号2'
	},
	{
		id: '3',
		consumeCode: '1235',
		equipPosition: '公司',
		materialCode: '物料编号3',
		equipCode: '设备编号3'
	}
]);

const indexList1 = ref([]);
const getEquipList = async (code) => {
	indexList1.value = [];
	// indexList.value.forEach((item) => {
	// 	if (item.consumeCode == code) {
	// 		indexList1.value.push(item);
	// 	}
	// });
	console.log('code', code);
	const res = await http({
		method: 'GET',
		url: `/eam/csmmaterialdata/getProduct/${code}`,
		data: {}
	})
		.then((res) => {
			if (res.data) {
				indexList1.value = [...res.data];
				console.log(indexList1.value);
			} else {
				uni.showToast({
					title: '该编号无设备信息，请重新扫描！'
				});
				return;
			}
		})
		.catch(() => {
			uni.showToast({
				title: '该编号无设备信息，请重新扫描！',
				icon: 'none'
			});
			// uni.navigateTo({
			// 	url: `/pages/eam/consumables/consumablesInstallation/index`,
			// 	animationDuration: 0
			// });
		});
};
const code = ref('');
if (props.id && props.code) {
	const result = props.id;
	code.value = props.code;
	getEquipList(result);
	console.log(result);
}

// const umodel = ref({
// 	id: "",
// 	unscheduledReportCode: "",
// 	unscheduledReportType: "",
// 	unscheduledReportContent: "",
// 	organizationId: "",
// 	organizationCode: "",
// 	plantId: "",
// 	plantCode: "",
// 	plantName: "",
// 	producePlanId: "",
// 	producePlanCode: "",
// 	produceOrderId: "",
// 	produceOrderCode: "",
// 	workCenterId: "",
// 	workCenterCode: "",
// 	workCenterName: "",
// 	createDateTime: Date.now(),
// 	createDate: "",
// 	crewId: "",
// 	crewName: "",
// 	shiftId: "",
// 	shiftCode: "",
// 	shiftName: "",
// 	startTime: 0,
// 	completeTime: 0,
// 	totalTime: 0,
// 	totalCost: 0,
// 	reportQuantity: 0,
// 	reportState: "",
// 	reportStateName: "新建",
// 	memberId: "",
// 	memberName: "",
// 	detailsPlan: [],
// 	detailsMember: [],
// })
const taskInfo = reactive({
	batchCount: 2,
	batchInfo: '001',
	consumeCode: '00182150272408160001',
	createTime: '2024-11-04 18:13:44',
	createdUserId: '1',
	createdUserName: 'admin',
	deleted: 0,
	enabled: '1',
	equipCode: 'TL',
	equipPosition: '01',
	equipPositionId: '1',
	id: '1853274170374483974',
	ids: null,
	lastUpdateTime: '2024-11-06 18:13:58',
	lastUpdatedUserId: '1',
	lastUpdatedUserName: 'admin',
	materialCode: '8215017\r',
	materialGroupCode: 'DGE360F4PHA5? drill(D3.6/5)\r',
	materialGroupId: '1',
	materialGroupName: 'DGE360F4PHA5? drill(D3.6/5)\r',
	materialId: '2',
	materialName: '简易砂轮',
	nodes: [
		{
			batchCount: 0,
			batchInfo: '001',
			consumeCode: '00182150272408160001',
			createTime: '2024-11-04 18:13:44',
			createdUserId: '1',
			createdUserName: 'admin',
			deleted: 0,
			enabled: '1',
			equipCode: 'TL',
			equipPosition: '01',
			equipPositionId: '1',
			id: '1853274170374483975',
			ids: null,
			lastUpdateTime: '2024-11-06 18:13:58',
			lastUpdatedUserId: '1',
			lastUpdatedUserName: 'admin',
			materialCode: '8215018',
			materialGroupCode: 'DGE360F4PHA5? drill(D3.6/5)\r',
			materialGroupId: '1',
			materialGroupName: 'DGE360F4PHA5? drill(D3.6/5)\r',
			materialId: '3',
			materialName: '可研磨砂轮',
			nodes: [],
			pid: '1853274170374483974',
			status: '1',
			workCount: 0,
			workLife: 200
		}
	],
	pid: '0',
	status: '1',
	workCount: 0,
	workLife: 200
});

// 选择订单
const selectOrder = ref({});
const selectIndex = ref();
const showImport = ref(false);
function selectWorkOrder(row, index) {
	showImport.value = true;
	selectOrder.value = row;
	selectIndex.value = index;
}
// 取消选择订单
function cancelImport() {
	showImport.value = false;
	selectOrder.value = {};
	selectIndex.value = null;
}
// 追加加工单
async function confirmImport() {
	showImport.value = false;

	// // 清空所有被选择状态
	// for (let i = 0; i < tableList.value.length; i++) {
	// 	tableList.value[i] = {
	// 		...tableList.value[i],
	// 		isSelect: false
	// 	};
	// }
	// // 更新给当前加工单状态
	// tableList.value[selectIndex.value] = {
	// 	...tableList.value[selectIndex.value],
	// 	isSelect: true
	// };

	uni.showToast({
		title: '选择成功'
	});
	console.log(selectOrder.value);
	uni.navigateTo({
		url: `/pages/eam/consumables/consumablesInstallation/index?result=${JSON.stringify(selectOrder.value)}&code=${code.value}`,
		animationDuration: 0
	});
}
async function loadmore(type?: string) {
	try {
		current.value += 1;
		const result: PageResult<any> = await http({
			method: 'POST',
			url: '/mes/mesmemberloginrecord/getWorkFormList',
			data: {
				current: current.value,
				size: 20,
				data: {
					workCenterId: loginWorkInfo.value.workCenterId,
					orderState: 1
				}
			}
		});
		const list = result.data;
		const selectId = workOrderList.value != [] ? workOrderList.value[0]?.id : '';
		for (let i = 0; i < list.length; i++) {
			tableList.value.push({ ...list[i], isSelect: selectId == list[i]?.id ? true : false });
		}
		totalPage.value = result.page;
		total.value = result.total;
		type === 'refresh' && uni.stopPullDownRefresh();
	} catch (e) {
		console.log(e);
	}
}
</script>

<style lang="scss" scoped>
.upload-scan {
	padding: 10px 40rpx;
}

.baseForm {
	background-color: #fff;
	.form {
		padding: $uni-page-padding;
	}

	.u-form-item {
		min-height: 46px;
	}

	:deep(.uicon-arrow-right) {
		top: 1px;
		margin-left: 3px;
	}

	.submit_btn {
		margin: 60rpx;
		border-radius: 60rpx;
	}

	:deep(.u-form-item__body__right__content__slot) {
		flex-direction: row-reverse;
	}

	:deep(.u-textarea__field) {
		text-align: right;
	}

	.u-textarea {
		border: 1px solid $uni-input-border-color;
		border-radius: 8px;
	}
}

.list {
	// :deep(.uni-scroll-view) {
	// 	overflow: visible !important;
	// }
	.card {
		margin: $uni-page-padding;
		margin-top: 5px;
		margin-bottom: 5px;
		border-radius: 7px;
		display: flex;
		align-items: center;
		padding: 10px 0;
		transition: all 300ms ease;
		box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);
		width: 90%;
		justify-content: space-between;

		.left {
			text-align: center;
			font-size: 13px;
			color: rgba(0, 0, 0, 0.5);
		}

		.right {
			.title {
				font-size: 15px;
				line-height: 20px;
				margin-bottom: 5px;
			}

			.body {
				color: rgba(238, 129, 48, 0.8);
				font-size: 13px;
				line-height: 18px;
			}
		}
	}
}

.u-list-item {
	margin: $uni-page-padding;
	margin-top: 5px;
	margin-bottom: 5px;
	border-radius: 7px;
	display: flex;
	align-items: center;

	transition: all 300ms ease;
	box-shadow: 0 1px 5px rgba(19, 11, 236, 0.13);
	background-color: #fff;
	display: flex;
	align-items: flex-start;

	.title {
		display: flex;

		.label {
			font-size: 14px;
			font-weight: 600;
			color: #101010;
		}

		.content {
			font-size: 14px;
		}
	}
}

.searchScan {
	display: flex;
	justify-content: space-between;
	align-items: center;
	height: 36px;
	width: 95%;
	padding: 6px;

	.addBtn1 {
		width: 60px;
		height: 36px;

		:deep(.u-button__text) {
			font-size: 14px !important;
		}
	}
}
</style>